package functions.ems_claim;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import coms.Constants;
import coms.process.LoginTest;
import frame.HttpHelper;
/**
 * 付款工作台-- 发起付款准备
 * @author CHENHAILIAN6
 *
 */
public class InitPaymentPreparation {
	private static Logger logger = LoggerFactory.getLogger(InitPaymentPreparation.class);
	String invoiceNo;
	String result;
    boolean isPassed = false;
    JSONArray initPaymentPreparations;
	public JSONArray getInitPaymentPreparations() {
		return initPaymentPreparations;
	}
	public void setInitPaymentPreparations(JSONArray initPaymentPreparations) {
	  initPaymentPreparations = initPaymentPreparations;
	}
	public boolean isPassed() {
		return isPassed;
	}
	public void setPassed(boolean isPassed) {
		this.isPassed = isPassed;
	}
	public InitPaymentPreparation(String invoiceNo) {
		this.invoiceNo = invoiceNo;
	}
	public InitPaymentPreparation() {
		// TODO Auto-generated constructor stub
	}
	/**
	 * 字段请款类型计算
	 */
	public void getPaymentMode() {
		logger.info("----------发起付款准备--请款类型-----开始--------");
		String url=Constants.GET_PAYMENT_MODE_URL+invoiceNo;
		try {
			result=HttpHelper.post("", url);
			logger.info("getPaymentMode"+result);
			//Swagger中暂时没有看到返回值，后续再写
			JSONObject jsonObject=new JSONObject(result);
			String status=jsonObject.getString("status").toString();
			if(status.equals("success")) {
				logger.info("查询成功");
			}
			
		}catch (Exception e) {
			// TODO: handle exception
		}
		logger.info("----------发起付款准备--请款类型-----结束--------");
	}
	
	public void findInitPaymentPreparationByCondition(HashMap<Object,Object> inputParams) {
		logger.info("findInitPaymentPreparationByCondition  --- start ---------------------");
		String currentPage = "1";
		String pageSize = "10";
		String payRequestOuCode = "";
		String vendorName = "";
		String startDueToDate = "";
		String endDueToDate = "";
		String payRequestClaimNo = "";
		String applyEmployeeName = "";
		String startCreateTime = "";
		String endCreateTime = "";
		String payMentMode = "";
		String auditDate = "";
		String vendorType = "";
		String startApplyAmount = "";
		String endApplyAmount = "";
		String startDraftAmount = "";
		String endDraftAmount = "";
		String status = "2";
		String projectCode = "";
		String currency = "";
		String fieldName = "";
		String orderBy = "";
		String invoiceNo = "";
				
		if(inputParams != null) {
			currentPage = (String) inputParams.get("currentPage");
			if(currentPage == null) {
				currentPage = "1";
			}
			pageSize = (String) inputParams.get("pageSize");
			if(pageSize == null) {
				pageSize = "10";
			}
			payRequestOuCode = (String) inputParams.get("payRequestOuCode");
			if(payRequestOuCode == null) {
				payRequestOuCode = "";
			}
			vendorName = (String) inputParams.get("vendorName");
			if(vendorName == null) {
				vendorName = "";
			}
			startDueToDate = (String) inputParams.get("startDueToDate");
			if(startDueToDate == null) {
				startDueToDate = "";
			}
			endDueToDate = (String) inputParams.get("endDueToDate");
			if(endDueToDate == null) {
				endDueToDate = "";
			}
			payRequestClaimNo = (String) inputParams.get("payRequestClaimNo");
			if(payRequestClaimNo == null) {
				payRequestClaimNo = "";
			}
			invoiceNo = (String) inputParams.get("invoiceNo");
			if(invoiceNo == null) {
				invoiceNo = "";
			}
			applyEmployeeName = (String) inputParams.get("applyEmployeeName");
			if(applyEmployeeName == null) {
				applyEmployeeName = "";
			}
			startCreateTime = (String) inputParams.get("startCreateTime");
			if(startCreateTime == null) {
				startCreateTime = "";
			}
			endCreateTime = (String) inputParams.get("endCreateTime");
			if(endCreateTime == null) {
				endCreateTime = "";
			}
			payMentMode = (String) inputParams.get("payMentMode");
			if(payMentMode == null) {
				payMentMode = "";
			}
			auditDate = (String) inputParams.get("auditDate");
			if(auditDate == null) {
				auditDate = "";
			}
			vendorType = (String) inputParams.get("vendorType");
			if(vendorType == null) {
				vendorType = "";
			}
			startApplyAmount = (String) inputParams.get("startApplyAmount");
			if(startApplyAmount == null) {
				startApplyAmount = "";
			}
			endApplyAmount = (String) inputParams.get("endApplyAmount");
			if(endApplyAmount == null) {
				endApplyAmount = "";
			}
			startDraftAmount = (String) inputParams.get("startDraftAmount");
			if(startDraftAmount == null) {
				startDraftAmount = "";
			}
		
			endDraftAmount = (String) inputParams.get("endDraftAmount");
			if(endDraftAmount == null) {
				endDraftAmount = "";
			}
			status = (String) inputParams.get("status");
			if(status == null) {
				status = "2";
			}
			projectCode = (String) inputParams.get("projectCode");
			if(projectCode == null) {
				projectCode = "";
			}
			currency = (String) inputParams.get("currency");
			if(currency == null) {
				currency = "";
			}
			fieldName = (String) inputParams.get("fieldName");
			if(fieldName == null) {
				fieldName = "";
			}
			orderBy = (String) inputParams.get("orderBy");
			if(orderBy == null) {
				orderBy = "";
			}
			
		}
		String params = "{\"page\":{\"currentPage\":"+currentPage+",\"pageSize\":"+pageSize+",\"recordCount\":0},"
				+ "\"params\":{\"payRequestOuCode\":\""+payRequestOuCode+"\",\"vendorName\":\""+vendorName+"\","
				+ "\"startDueToDate\":\""+startDueToDate+"\",\"endDueToDate\":\""+endDueToDate+"\","
				+ "\"payRequestClaimNo\":\""+payRequestClaimNo+"\",\"invoiceNo\":\""+invoiceNo+"\","
				+ "\"applyEmployeeName\":\""+applyEmployeeName+"\",\"startCreateTime\":\""+startCreateTime+"\","
				+ "\"endCreateTime\":\""+endCreateTime+"\",\"payMentMode\":\""+payMentMode+"\",\"auditDate\":\""+auditDate+"\","
				+ "\"vendorType\":\""+vendorType+"\",\"startApplyAmount\":\""+startApplyAmount+"\",\"endApplyAmount\":\""+endApplyAmount+"\","
				+ "\"startDraftAmount\":\""+startDraftAmount+"\",\"endDraftAmount\":\""+endDraftAmount+"\",\"status\":\""+status+"\","
				+ "\"projectCode\":\""+projectCode+"\",\"currency\":\""+currency+"\"},"
				+ "\"sortDTO\":{\"fieldName\":\""+fieldName+"\",\"orderBy\":\""+orderBy+"\"}}";
		logger.info(params);
		String url = Constants.FIND_INIT_PAYMENT_PREPARATION_BY_CONDITION_URL;
		try {
			result = HttpHelper.post(params, url);
			isPassed = HttpHelper.isOk;
			logger.info("findInitPaymentPreparationByCondition--------"+ result);
			if(isPassed) {
				try {
					JSONObject datas = new JSONObject(result);
					int total = datas.getInt("total");
					if(total >0) {
						try {
						  initPaymentPreparations = datas.getJSONArray("list");
							for(int i = 0;i<initPaymentPreparations.length();i++) {
								JSONObject object = initPaymentPreparations.getJSONObject(i);
								logger.info(object.toString());
							}
						
						}catch(JSONException e) {
							e.printStackTrace();
						}
						
					}
				}catch(Exception e) {
					e.printStackTrace();
					isPassed = false;
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		logger.info(" 返回结果  ------" + isPassed);
		logger.info("findInitPaymentPreparationByCondition  --- end ---------------------");
	}
	
	public static void main(String[] args) {
	
	}
	
}
